package com.lagou.niancheng.rpc.client.netty.loadbalance;

import com.lagou.niancheng.rpc.common.entity.HostPortMetaData;
import com.lagou.niancheng.rpc.client.netty.Loadbalancer;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 轮询方式负载
 *
 * @author 大犬
 * @date 2020/5/12
 */
public class RoundRobinLoadbalancer implements Loadbalancer {

    private AtomicInteger index = new AtomicInteger(0);

    @Override
    public HostPortMetaData balance(List<HostPortMetaData> hostPortMetaDataList) {
        int idx = index.incrementAndGet();
        return hostPortMetaDataList.get(idx % hostPortMetaDataList.size());
    }
}
